#pragma once
#include <sys/wait.h>
#include <sys/socket.h>

enum plugin_state_t {
    STATE_UNLOADED,
    STATE_LOADING,
    STATE_RUNNING,
    STATE_BLACKOUT,
    STATE_DEAD,
    STATE_RELOADING
};

struct plugin_t {
    int fd;
    pid_t pid;
    char *file;
    char *name;
    char *desc;
    int ver;
    int priority;
    int initialized;
    int mortal;
    enum plugin_state_t state;
    struct command *commands;
    struct plugin_t *next;
};

typedef struct plugin_t plugin;
typedef enum plugin_state_t plugin_state;

int plugin_init(const char *file, int priority);
int plugin_kill(plugin*);
int murder(pid_t pid);
int plugin_info(plugin*, plugin*);
int plugins_init(void);
char *plugin_to_str(plugin*);
plugin *name_to_plugin(const char *name);
plugin *file_to_plugin(const char *file);
plugin *get_prev_plugin(plugin*);
plugin *fd_to_plugin(int fd);
plugin *find_smaller_fd(int fd);
plugin *pid_to_plugin(pid_t pid);
int plugin_setinfo(plugin *p, const char *name, const char *desc, const int ver);

void cmd_plugin_init(plugin*, const char **argv);
void cmd_plugin_kill(plugin*, const char **argv);
void cmd_plugin_list(plugin*);
void cmd_plugin_state(plugin*, const char **argv);
void cmd_plugin_setinfo(plugin*, const char **argv);
void cmd_plugin_getinfo(plugin*, const char **argv);

extern plugin *plugins;
